home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / PCASM2.ZIP / CHAP0-1.DOC < prev    next >
Text File  |  1990-08-04  |  42KB  |  897 lines

  1.  
  2.  
  3.  
  4.                                                                              i
  5.  
  6.                           CHAPTER 0.1 - NUMBERS AND ARITHMETIC
  7.  
  8.  
  9.              You don't habitually use the base two system to balance your
  10.              checkbook, so it would be counterproductive to teach you machine
  11.              arithmetic on a base two system. What number systems have you had
  12.              a lot of experience with? The base 10 system springs to mind. I'm
  13.              going to show you what happens on a base 10 system so you will
  14.              understand the structure of what happens with computer
  15.              arithmetic.
  16.  
  17.  
  18.              BASE 10 MACHINE
  19.  
  20.              Each place inside the microprocessor that can hold a number is
  21.              called a REGISTER. Normally there are a dozen or so of these. Our
  22.              base 10 machine has 4 digit registers.  They can represent any
  23.              number from 0000 to 9999. They are exactly like an industrial
  24.              counters or the counters on your tape machines.{1} If you add 27
  25.              to a register, the microprocessor counts forward 27; if you
  26.              subtract 153 from a register, the microprocessor counts backwards
  27.              153.   Every time you add 1 to a register, it increments by 1 -
  28.              that is 0245, 0246, 0247, 0248. Every time you subtract 1 from a
  29.              register, it decrements by 1 - that is 3480, 3479, 3478, 3477.
  30.  
  31.              Let's do some more incrementing.  9997, 9998, 9999, 0000, 0001,
  32.              0002. Whoops! That's a problem. When the register reaches 9999
  33.              and we add 1, it changes to 0000, not 10,000. How can we tell the
  34.              difference between 0000 and 10,000? We can't without a little
  35.              help from the CPU.{2}  Immediately after an arithmetical
  36.              operation, the CPU knows whether you have gone through 10,000
  37.              (9999->0000). The CPU has something called a carry flag. It is
  38.              internal to the CPU and can have the value 0 or 1. After each
  39.              arithmetical operation, the CPU sets the CARRY FLAG to 1 if you
  40.              went through the 9999/0000 boundary, and sets the carry flag to 0
  41.              if you didn't.{3}
  42.  
  43.              Here are some examples, showing addition, the result, and the
  44.              carry flag. The carry flag is normally abbreviated by CF.
  45.  
  46.                     number 1       number 2        result     CF
  47.  
  48.                       0289           4782           5071      0 
  49.                       4398           2964           7382      0
  50.                       8177           5826           4003      1
  51.              ____________________
  52.  
  53.                 1. Exactly like industrial counters that have several hundred
  54.              thousand parts, that is.
  55.  
  56.                 2. The CPU (central processing unit) is the chip(s) that does
  57.              all the arithmetic. In the case of the PC, it is the 8086.
  58.  
  59.                 3. When you set a flag to 0, it is called CLEARING the flag.
  60.  
  61.              ______________________
  62.  
  63.              The PC Assembler Tutor - Copyright (C) 1989 Chuck Nelson
  64.  
  65.  
  66.  
  67.  
  68.              The PC Assembler Tutor                                         ii
  69.              ______________________
  70.  
  71.                       6744           4208           0952      1
  72.  
  73.              Note that you must check the carry flag immediately after the
  74.              arithmetical operation. If you wait, the CPU will reset it after
  75.              the next arithmetical operation.
  76.  
  77.              Now let's do some decrementing. 0003, 0002, 0001, 0000, 9999,
  78.              9998. Golly gosh! Another problem. When we got to 0000, rather
  79.              than getting -1, -2, we got 9999, 9998. Apparently 9999 stands
  80.              for -1, 9998 stands for -2. Yes, that's the system on this, on
  81.              the 8086, and on all computers. (Back to that in a moment.) How
  82.              do we tell that the number went through 0 ; i.e. 0000->9999? The
  83.              carry flag comes to the rescue again. If the number goes through
  84.              the 9999/0000 boundary in either direction, the CPU sets the CF
  85.              to 1; if it doesn't, the CPU sets the CF to 0. Here's some
  86.              subtraction, with the result and the carry flag.
  87.  
  88.                     number 1       number 2       result     CF
  89.  
  90.                       8473           2752           5721      0
  91.                       2836           4583           1747      1
  92.                       0654           9281           8627      1
  93.                       9281           0654           8627      0
  94.  
  95.              Look at examples 3 and 4. The numbers are reversed. The results
  96.              are the same but they have different signs. But that is as it
  97.              should be. When you reverse the order in a subtraction, you get
  98.              the same absolute value, only a different sign (15 - 7 = 8 but 
  99.              7 - 15 = -8). Remember, the CF is reliable only immediately after
  100.              the operation.
  101.  
  102.  
  103.              NEGATIVE NUMBERS
  104.  
  105.              The negative numbers go 9999=-1, 9998=-2, 9997=-3, 9996=-4,
  106.              9995=-5 etc. A more negative number is denoted by a smaller
  107.              number in the register; -5 = 10,000 -5 = 9995; -498 = 10,000 -498
  108.              = 9502, and in general, -x = 10,000 -x. Here are some negative
  109.              numbers and their representations on our machine.
  110.  
  111.                      number     machine no              number     machine no
  112.  
  113.                         -27          9973                -4652          5348
  114.                       -8916          1084                -6155          3845
  115.  
  116.              As you will notice, these numbers look exactly the same as the
  117.              unsigned numbers. They ARE exactly the same as the unsigned
  118.              numbers. The machine has no way of knowing whether a number in a
  119.              register is signed or unsigned. Unlike BASIC or PASCAL which will
  120.              complain whenever you try to use a number in an incorrect way,
  121.              the machine will let you do it. This is the power and the curse
  122.              of machine language. You are in complete control. It is your
  123.              responsibility to keep track of whether a number is signed or
  124.              unsigned. 
  125.  
  126.              Which signed numbers should be positive and which negative? This
  127.              has already been decided for you by the computer, but let's think
  128.  
  129.  
  130.  
  131.  
  132.              Chapter 0.1 - Numbers and Arithmetic                          iii
  133.              ____________________________________
  134.  
  135.              out what a reasonable solution might be. We could have from 0000
  136.              to 8000 positive and from 9999 to 8001 negative, but that would
  137.              give us 8001 positive numbers and 1999 negative numbers. That
  138.              seems unbalanced. More importantly, if we take -(3279) the
  139.              machine will give us 6721, which is a POSITIVE number. We don't
  140.              want that. For reasons of symmetry, the positive numbers are
  141.              0000-4999 and the negative numbers are 9999-5000.{4} Our most
  142.              negative number is -5000 = 10,000 -5000 = 5000.
  143.  
  144.  
  145.              10'S COMPLEMENT
  146.  
  147.              It's time for a digression. If we are going to be using negative
  148.              numbers like -(473), changing from an external number to an
  149.              internal number is going to be a bother: i.e. -473 -> 9527. Going
  150.              the other way is going to be a pain too: i.e. 9527 -> -473. Well,
  151.              it would be a problem except that we have some help.
  152.  
  153.                  0000 =    10,000    =     9999     +1
  154.                                           - 473
  155.              result                        9526     +1   = 9527
  156.  
  157.              Let's work this through carefully. On our machine, 0000  and
  158.              10000 (9999+1) are the same thing, so 0 - 473 is the same as
  159.              9999+1-473 which is the same as 9999-473+1. But when we have all
  160.              9s, this is a cinch. We never have to borrow - all we have to do
  161.              is subtract each digit from 9 and then add 1 to the total. We may
  162.              have to carry at the end, but that is a lot better than all those
  163.              borrows. We'll do a few examples: 
  164.  
  165.              (-4276)
  166.                  0000 =    10,000    =     9999     +1
  167.                                           -4276
  168.              result                        5723     +1   = 5724
  169.  
  170.  
  171.              (-3982)
  172.                  0000 =    10,000    =     9999     +1
  173.                                           -3982
  174.              result                        6017     +1   = 6018
  175.  
  176.  
  177.              (-2400)
  178.                  0000 =    10,000    =     9999     +1
  179.                                           -2400
  180.              result                        7599     +1   = 7600
  181.  
  182.  
  183.              (-1989)
  184.                  0000 =    10,000    =     9999     +1
  185.              ____________________
  186.  
  187.                 4. That way, if we tell the machine that we are working with
  188.              signed numbers, all it has to do is look at the left digit. If
  189.              the digit is 5-9, we have a negative number, if it is 0-4, we
  190.              have a positive number. Note that 0000 is considered to be
  191.              positive. This is true on all computers.
  192.  
  193.  
  194.  
  195.  
  196.              The PC Assembler Tutor                                         iv
  197.              ______________________
  198.  
  199.                                           -1989
  200.              result                        8010     +1   = 8011
  201.  
  202.              This is called 10s complement. Subtract each digit from 9, then
  203.              add 1 to the total. One thing we should check is whether we get
  204.              the same number back if we negate the negative result; i.e. does
  205.              -(-1989)) = 1989?  From the last example, we see that -1989 =
  206.              8011, so:
  207.  
  208.              (-8011)
  209.                  0000 =    10,000    =     9999     +1
  210.                                           -8011
  211.              result                        1988     +1   = 1989
  212.  
  213.              It seems to work. In fact, it always works. See the footnote for
  214.              the proof.{5} You are going to use this from time to time, so you
  215.              might as well practice some. Here are 10 numbers to put into 10s
  216.              complement form. The answers are in the footnote. (1) -628, (2)
  217.              -4194, (3) -9983, (4) -1288, (5) -4058, (6) -6952, (7) -162, (8)
  218.              -9, (9) -2744, (10) -5000.{6}
  219.  
  220.  
  221.              The computer keeps track of whether a number is positive or
  222.              negative. After an arithmetical operation, it sets a flag to tell
  223.              whether the result is positive or negative. This flag has no
  224.              meaning if you are using unsigned numbers. The computer is
  225.              saying, "If the last arithmetical operation was with signed
  226.              numbers, then this is the sign of the result." The flag is called
  227.              the sign flag (SF). It is 0 if the number is positive and 1 if
  228.              the number is negative. Let's decrement again and look at both
  229.              the sign flag and carry flag.
  230.  
  231.                         NUMBER    SIGN     CARRY
  232.  
  233.                            3         0         0
  234.                            2         0         0
  235.                            1         0         0
  236.                            0         0         0
  237.                         9999         1         1
  238.              ____________________
  239.  
  240.                 5. Let x be any number. Then:
  241.              -x     = ( 10,000 - x)     = ( 9999 + 1 - x ) ;
  242.  
  243.              -(-x)  = ( 10,000 - (-x) ) = ( 9999 + 1 - (-x) )
  244.                                        = ( 9999 + 1 - ( 9999 + 1 - x ) )
  245.                                        = ( 9999 + 1 - 9999 - 1 + x )
  246.                                        = x
  247.  
  248.                 6.   (1) -628 = 9372 , (2) -4194 = 5806 , (3) -9983 = 0017,
  249.                      (4) -1288 = 8712 , (5) -4058 = 5942 , (6) -6952 = 3048
  250.                      (7) -162 = 9838 , (8) -9 = 9991 , (9) -2744 = 7256,
  251.                      (10) -5000 = 5000. This last one is a little strange. It
  252.              changes 5000 into itself. In our system, 5000 is a negative
  253.              number and it winds up as a negative number. This happens on all
  254.              computers. If you take the maximum negative number and take its
  255.              negative, you get the same number back.
  256.  
  257.  
  258.  
  259.  
  260.              Chapter 0.1 - Numbers and Arithmetic                            v
  261.              ____________________________________
  262.  
  263.                         9998         1         0
  264.                         9997         1         0
  265.                         9996         1         0
  266.  
  267.              That worked pretty well. The sign flag changed from 0 to 1 when
  268.              we went from 0 to 9999 and the carry flag was set to 1 for that
  269.              one operation so we could see that we had gone through the
  270.              9999/0000 boundary. 
  271.  
  272.              Let's do some more decrementing.
  273.  
  274.                         NUMBER    SIGN     CARRY
  275.  
  276.                         5003         1         0
  277.                         5002         1         0
  278.                         5001         1         0
  279.                         5000         1         0
  280.                         4999         0         0
  281.                         4998         0         0
  282.                         4997         0         0
  283.                         4996         0         0
  284.  
  285.              This one didn't work too well. 5000 is our most negative number
  286.              (-5000) and 4999 is our most positive number; when we crossed the
  287.              4999/5000 boundary, the sign changed but there was nothing to
  288.              tell us that the sign had changed. We need to make another flag.
  289.              This one is called the overflow flag. We check the carry flag
  290.              (CF) for the 0000/9999 boundary and we check the overflow flag
  291.              for the 5000/4999 boundary. The last decrementing example with
  292.              the overflow flag:
  293.  
  294.                         NUMBER    SIGN     CARRY     OVERFLOW
  295.  
  296.                         5003         1         0         0
  297.                         5002         1         0         0
  298.                         5001         1         0         0
  299.                         5000         1         0         0
  300.                         4999         0         0         1
  301.                         4998         0         0         0
  302.                         4997         0         0         0
  303.                         4996         0         0         0
  304.  
  305.              This time we can find out that we have gone through the boundary.
  306.              We'll come back to how the computer sets the overflow flag later,
  307.              but let's do some addition and subtraction now.
  308.  
  309.  
  310.              UNSIGNED ADDITION AND SUBTRACTION
  311.  
  312.              Unsigned addition is done the same way as normally. The computer
  313.              adds the two numbers. If the result is over 9999, it sets the
  314.              carry flag and drops the left digit (i.e. 14625 -> 4625, CF = 1,
  315.              19137 -> 9137 CF = 1, 10000 -> 0000 CF = 1). The largest possible
  316.              addition is 9999 + 9999 = 19998. This still has a 1 in the left
  317.              digit. If the carry flag is set after an addition, the result
  318.              must be between 10000 and 19998.
  319.  
  320.  
  321.  
  322.  
  323.  
  324.              The PC Assembler Tutor                                         vi
  325.              ______________________
  326.  
  327.              Since this is unsigned addition, we won't worry about the sign
  328.              flag or the overflow flag for the moment. Here are some examples
  329.              of unsigned addition.
  330.  
  331.                       NUMBER 1       NUMBER 2       RESULT         CF
  332.  
  333.                         5147           2834          7981           0
  334.                         6421           8888          5309           1
  335.                         2910           6544          9454           0
  336.                         6200           6321          2521           1
  337.  
  338.              Directly after the addition, the computer has complete
  339.              information about the number. If the carry flag is set, that
  340.              means that there is an extra 10,000, so the result of the second
  341.              example is 15309 and the result of the fourth example is 12521. 
  342.              There is no way to store all that information in 4 digits in
  343.              memory so that extra information will be lost if it is not used
  344.              immediately. 
  345.  
  346.              Subtraction is similar. The machine subtracts, and if the answer
  347.              is below 0000, it sets the carry flag, borrows 10000 and adds it
  348.              to the result. -3158 -> -3135 + 10000 -> 6842 CF = 1 ; -8197 ->
  349.              -8197 + 10000 -> 1803  CF = 1. After a subtraction, if the carry
  350.              flag is set, you know the number is 10000 too big. Once again,
  351.              the carry flag information must be used immediately or it will be
  352.              lost. Here are some examples:
  353.  
  354.                       NUMBER 1       NUMBER 2       RESULT         CF
  355.  
  356.                         3872           2655          1217           0
  357.                         9826           5967          3859           0
  358.                         4561           7143          7418           1
  359.                         2341           4907          7434           1
  360.  
  361.              If the carry flag is set, the computer borrowed 10000, so example
  362.              3 is 7418 - 10000 = -2582 and example 4 is 7434 - 10000 = -2566.
  363.  
  364.  
  365.              MODULAR ARITHMETIC
  366.  
  367.              What the computer is doing is modular arithmetic. Modular
  368.              arithmetic is like a clock. If it is 11 o'clock and you go
  369.              forward 1 hour it's now 12 o'clock; if it's 11 and you go
  370.              backwards 1 hour it's now 10. If it's 11 and you go forward 4
  371.              hours it's not 15, it's 3. If it's 11 and you go backward 15
  372.              hours it's not -4, it's 8. 
  373.  
  374.              The clock is doing  mod 12  arithmetic.{7} 
  375.  
  376.                  (A+B) mod 12
  377.                  (A-B) mod 12
  378.  
  379.              From the clock's viewpoint, 11 o'clock today, 11 o'clock
  380.              yesterday and 11 o'clock, June 8, 1754 are all the same thing. If
  381.              ____________________
  382.  
  383.                 7. To be a perfect analogy 12 o'clock should be 0 o'clock.
  384.  
  385.  
  386.  
  387.  
  388.              Chapter 0.1 - Numbers and Arithmetic                          vii
  389.              ____________________________________
  390.  
  391.              you go forward 200 hours (that's 12X16 + 8) you will have the
  392.              same result as going forward 8 hours. If you go backwards 200
  393.              hours (that's -(12X16 + 8) = -(12X16) -8) you get the same result
  394.              as going backwards 8 hours. If you go forward 4 hours from 11
  395.              (11+4) mod 12 = 3 you get the same result as going backwards 8
  396.              hours (11-8) mod 12 = 3. In fact, these come in pairs. If A + B =
  397.              12, then going forward A hours gives the same result as going
  398.              backwards B hours. Forwards 9 = backwards 3; forwards 7 =
  399.              backwards 5; forwards 11 = backwards 1.
  400.  
  401.              In the mod 12 system, the following things are equivalent:
  402.  
  403.                  (+72 + 4)      (+72 - 8)
  404.                  (+60 + 4)      (+60 - 8)
  405.                  (+48 + 4)      (+48 - 8)
  406.                  (+36 + 4)      (+36 - 8)
  407.                  (+24 + 4)      (+24 - 8)
  408.                  (+12 + 4)      (+12 - 8)
  409.                  (  0 + 4)      (  0 - 8)
  410.                  (-12 + 4)      (-12 - 8)
  411.                  (-24 + 4)      (-24 - 8)
  412.                  (-36 + 4)      (-36 - 8)
  413.                  (-48 + 4)      (-48 - 8)
  414.                  (-60 + 4)      (-60 - 8)
  415.  
  416.              They form what is known as an equivalence class mod 12. If you
  417.              use any one of them for addition or subtraction, you will get the
  418.              same result (mod 12) as with any other one. Here's some
  419.              addition:{8}
  420.  
  421.                  (+48 + 4) + 7 = (48 + 11) mod 12 = 11
  422.                  (-48 - 8) + 7 = (48 - 1 ) mod 12 = 11
  423.                  (  0 - 8) + 7 = ( 0 - 1 ) mod 12 = 11
  424.                  (-60 + 4) + 7 = (-60 +11) mod 12 = 11
  425.  
  426.              And some subtraction:
  427.  
  428.                  (+48 + 4) - 2 = (48 + 2 ) mod 12 = 2
  429.                  (-48 - 8) - 2 = (48 - 10) mod 12 = 2
  430.                  (  0 - 8) - 2 = ( 0 - 10) mod 12 = 2
  431.                  (-60 + 4) - 2 = (-60 + 2) mod 12 = 2
  432.  
  433.  
  434.              Our pretend computer doesn't cycle every 12 numbers, it cycles
  435.              every 10,000 numbers - it is a mod 10,000 machine. On our
  436.              machine, the number 6453 has the following equivalence class:
  437.  
  438.                  (+30000 + 6453)               (+30000 - 3547)
  439.                  (+20000 + 6453)               (+20000 - 3547)
  440.                  (+10000 + 6453)               (+10000 - 3547)
  441.                  (     0 + 6453)               (     0 - 3547)
  442.                  (-10000 + 6453)               (-10000 - 3547)
  443.                  (-20000 + 6453)               (-20000 - 3547)
  444.                  (-30000 + 6453)               (-30000 - 3547)
  445.              ____________________
  446.  
  447.                 8. (-10) mod 12 = 2 ;   (-11) mod 12 = 1
  448.  
  449.  
  450.  
  451.  
  452.              The PC Assembler Tutor                                       viii
  453.              ______________________
  454.  
  455.  
  456.              Any one of these will act the same as any other one. Notice that
  457.              10000 - 3547 is the subtraction that we did to get the
  458.              representation of -3547 on the machine. 
  459.  
  460.              -3547    = 9999 + 1
  461.                         3547
  462.                         6452 + 1 = 6453
  463.  
  464.              6453 and -3547 act EXACTLY the same on this machine. What this
  465.              means is that there is no difference in adding signed or unsigned
  466.              numbers on the machine. The result will be correct if interpreted
  467.              as an unsigned number; it will also be correct if interpreted as
  468.              a signed number.
  469.  
  470.                  6821 + 3179 = 10000  so  -3179 = 6821   and  3179 = -6821
  471.                  5429 + 4571 = 10000  so  -4571 = 5429   and  4571 = -5429
  472.  
  473.              Since -3179 and 6821 act the same on our machine and since -4571
  474.              and 5429 act the same, let's do some addition. Take your time so
  475.              you understand why the signed and unsigned numbers are giving the
  476.              same results mod 10000:
  477.  
  478.                   ---------------------------------------------------------
  479.                   6821 + 497 = 7318
  480.                  -3179 + 497 = (10000 - 3179) + 497 = 10000 -2682  = -2682
  481.  
  482.                   7318 + 2682 = 10000      so    -2682 = 7318
  483.  
  484.                   ----------------------------------------------------------
  485.  
  486.                   5429 + 876 = 6305
  487.                  -4571 + 876 = (10000 - 4571) + 876 = 10000 - 3695 = -3695
  488.  
  489.                   6305 + 3695 = 10000      so    -3695 = 6305
  490.  
  491.                   ----------------------------------------------------------
  492.  
  493.              Here's some subtraction:
  494.  
  495.                   -----------------------------------------------------------
  496.  
  497.                   6821 - 507 = 6314
  498.                  -3179 - 507 = (10000 - 3179) - 507 = 10000 - 3686 = -3686
  499.  
  500.                   6314 + 3686 = 10000     so     -3686 = 6314
  501.  
  502.                   ----------------------------------------------------------
  503.  
  504.                   5429 - 178 = 5251
  505.                  -4571 - 178 = (10000 - 4571) - 178 = 10000 - 4749 = -4749
  506.  
  507.                   5251 + 4749 = 10000    so      -4749 = 5251
  508.  
  509.                   -----------------------------------------------------------
  510.  
  511.              It is the same addition or subtraction. Interpreted one way it is
  512.  
  513.  
  514.  
  515.  
  516.              Chapter 0.1 - Numbers and Arithmetic                           ix
  517.              ____________________________________
  518.  
  519.              signed addition or subtraction; interpreted another way it is
  520.              unsigned addition or subtraction.
  521.  
  522.              The machine could have one operation for signed addition and
  523.              another operation for unsigned addition, but this would be a
  524.              waste of computer resources. These operations are exactly the
  525.              same. This machine, like all computers, has only one integer
  526.              addition operation and one integer subtraction operation. For
  527.              each operation, it sets the flags of importance for both signed
  528.              and unsigned arithmetic.
  529.  
  530.              For unsigned addition and subtraction, CF, the carry flag tells
  531.              whether the 0000/9999 boundary has been crossed.
  532.  
  533.              For signed addition and subtraction, SF, the sign flag tells the
  534.              sign of the result and OF, the overflow flag tells whether the
  535.              result was too negative or too positive.
  536.  
  537.  
  538.              SIGN EXTENSION
  539.  
  540.              Although our base 10 machine is set up for 4 digit numbers, it is
  541.              possible to use it for numbers of any size by writing the
  542.              appropriate software. We'll use 12 digit numbers as an example,
  543.              though they could be of any length. The first problem is
  544.              converting 4 digit numbers into 12 digit numbers. If the number
  545.              is an unsigned number, this is no problem (we'll write the number
  546.              in groups of 4 digits to keep it readable):
  547.  
  548.                  4816      ->   0000 0000 4816
  549.                  9842      ->   0000 0000 9842
  550.                   127      ->   0000 0000 0127
  551.  
  552.              what if it is a signed number? The first thing we need to know
  553.              about signed numbers is, what is positive and what is negative?
  554.              Once again, for reasons of symmetry, we choose positive to be 
  555.              0000 0000 0000  to  4999 9999 9999 and negative to be 5000 0000
  556.              0000 to 9999 9999 9999.{9}  This longer number system cycles from
  557.  
  558.              9999 9999 9999 to 0000 0000 0000. Therefore, for longer numbers,
  559.              0000 0000 0000 = 1 0000 0000 0000. They are equivalent. 
  560.              0000 0000 0000 = 9999 9999 9999 + 1.
  561.  
  562.              If it is a positive signed number, it is still no problem (recall
  563.              that in our 4 digit system, a positive number is between 0000 and
  564.              4999, a negative signed number is between 5000 and 9999). Here
  565.              are some positive signed numbers and their conversions:
  566.  
  567.                  1974      ->   0000 0000 1974
  568.                     1      ->   0000 0000 0001
  569.                  3909      ->   0000 0000 3909
  570.  
  571.              ____________________
  572.  
  573.                 9. Once again, the sign will be decided by the left hand
  574.              digit. If it is 0-4 it is a positive number; if it is 5-9 it is a
  575.              negative number.
  576.  
  577.  
  578.  
  579.  
  580.              The PC Assembler Tutor                                          x
  581.              ______________________
  582.  
  583.              If it is a negative number, where did its representation come
  584.              from in our 4 digit system? -x -> 9999 + 1 -x = 9999 - x + 1.
  585.              This time it won't be 9999 + 1 but 9999 9999 9999 + 1. Let's have
  586.              some examples.
  587.  
  588.                  4 DIGIT SYSTEM       12 DIGIT SYSTEM
  589.  
  590.              -1964
  591.                   9999     + 1        9999 9999 9999 + 1
  592.                  -1964                         -1964
  593.                   8035   -> 8036      9999 9999 8035 + 1 -> 9999 9999 8036
  594.  
  595.              -2867
  596.                   9999     + 1        9999 9999 9999 + 1
  597.                  -2867                         -2867
  598.                   7132   -> 7133      9999 9999 7132 + 1 -> 9999 9999 7133
  599.  
  600.              -182
  601.                   9999     + 1        9999 9999 9999 + 1
  602.                   -182                          -182
  603.                   9817   -> 9818      9999 9999 9817 + 1 -> 9999 9999 9818
  604.  
  605.              As you can see, all you need to do to sign extend a negative
  606.              number is to put 9s to the left. 
  607.  
  608.              Can't those 9s on the left become 0s when we add that 1 at the
  609.              end?  No. In order for that to happen, the right four digits must
  610.              be 9999. But that can only happen if the number to be negated is
  611.              0000:
  612.  
  613.                   9999 9999 9999 + 1
  614.                            -0000
  615.                   9999 9999 9999 + 1 -> 0000 0000 0000
  616.  
  617.              In all other cases, adding 1 does not carry anything out of the
  618.              right four digits.
  619.  
  620.  
  621.              It is impossible to truncate one of these 12 digit numbers to a 4
  622.              digit number without making the results unreliable. Here are two
  623.              examples:
  624.  
  625.              (number)      0000 0168 7451 ->   7451  (now a negative number)
  626.              (actual value)     +168 7451     -2549 
  627.  
  628.              (number)      9999 9643 2170 ->   2170  (now a positive number)
  629.              (actual value)     -356 7830     +2170
  630.  
  631.  
  632.              We now have 12 digit numbers. Is it possible to add them and
  633.              subtract them? Yes but only 4 digits at a time. When you add with
  634.              pencil and paper you carry left from each digit. The computer can
  635.              carry left from each group of 4 digits. We'll do the following
  636.              addition:
  637.  
  638.                            0138 6715 6037
  639.                          + 2514 2759 7784
  640.  
  641.  
  642.  
  643.  
  644.              Chapter 0.1 - Numbers and Arithmetic                           xi
  645.              ____________________________________
  646.  
  647.  
  648.              Do this with pencil and paper and write down all the carries. The
  649.              computer is going to do this in 3 parts:
  650.  
  651.                  1) 6037 + 7784
  652.                  2) 6715 + 2759 + carry (if any)
  653.                  3) 0138 + 2514 + carry (if any)
  654.  
  655.              The first addition is our regular addition. It will set the carry
  656.              flag if the 0000/9999 boundary was crossed (i.e. the result was
  657.              larger than 9999). In our case CF = 1 since the result is 13821.
  658.              The register holds 3821. We store 3821. Next, we need to add
  659.              three things: 6715 + 2759 + CF (=1). There is an instruction like
  660.              this on all computers. It adds two numbers plus the value of the
  661.              carry flag. Our first addition was ADD (add two numbers). This
  662.              time the machine instruction is ADC (add two numbers and the
  663.              carry). The result of our second addition is 9475. The register
  664.              holds 9475 and CF = 0. We store 9475. Finally, we need to add
  665.              three more things: 0138 + 2514 + CF (=0). Once again we use ADC.
  666.              The result is 2652, CF = 0. We store the 2652. That is the whole
  667.              result:
  668.  
  669.                  2652 9475 3821
  670.  
  671.              If CF = 1 at this point, the number has crossed the
  672.              9999,9999,9999/0000,0000,0000 boundary. This will work for signed
  673.              numbers also. The only difference is that at the very end we
  674.              don't check CF, we check OF to see if the
  675.              4999,9999,9999/5000,0000,0000 boundary has been crossed. 
  676.  
  677.  
  678.              Just to give you one more example we'll do a subtraction using
  679.              the same numbers:
  680.  
  681.                            0138 6715 6037
  682.                            2514 2759 7784
  683.  
  684.              Notice that in order for you to do this with pencil and paper
  685.              you'll have to put the larger number on top before you subtract.
  686.              With the machine this is unnecessary. Go ahead and do the
  687.              subtraction with pencil and paper.
  688.  
  689.              The machine can do this 4 digits at a time, so this is a three
  690.              step process:
  691.  
  692.                  1) 6037 - 7784 
  693.                  2) 6715 - 2759 - borrow (if any)
  694.                  3) 0138 - 2514 - borrow (if any)
  695.  
  696.              The first one is a regular subtraction and since the bottom
  697.              number is larger, the result is 8253, CF = 1. (Perhaps you are
  698.              puzzled because that's not the result that you got. Don't worry,
  699.              it all comes out in the wash). Step two subtracts but also
  700.              subtracts any borrow (We had a borrow because CF = 1). There is a
  701.              special instruction called SBB (subtract with borrow) that does
  702.              just that. 6715 - 2759 - 1 = 3955, CF = 0. We store the 3955 and
  703.              go on to the third part. This also is SBB, but since we had no
  704.  
  705.  
  706.  
  707.  
  708.              The PC Assembler Tutor                                        xii
  709.              ______________________
  710.  
  711.              borrow, we have 0138 - 2514 - 0 = 7624, CF = 1. We store 7624.
  712.              This is the end result, and since CF = 1, we have crossed the
  713.              9999,9999,9999/0000,0000,0000 boundary. This is going to be the
  714.              representation of a negative number mod 1,0000,0000,0000. With
  715.              pencil and paper, your result was:
  716.  
  717.                  -2375 6044 1747
  718.  
  719.              The machine result was:
  720.  
  721.                   7624 3955 8253
  722.  
  723.              But CF was 1 at the end, so this represents a negative number.
  724.              What number does it represent? Let's take its negative to get a
  725.              positive number with the same absolute value:
  726.  
  727.                  9999 9999 9999  + 1
  728.                  7624 3955 8253
  729.                  2375 6044 1746  + 1  = 2375 6044 1747
  730.  
  731.              This is the same thing you got with pencil and paper. The reason
  732.              it looked wierd is that a negative number is always stored as its
  733.              modular equivalent. If you want to read a negative number, you
  734.              need to take its negative to get a positive number with the same
  735.              absolute value.
  736.  
  737.              If we had been working with signed numbers, we wouldn't have
  738.              checked CF at the very end, we would have checked OF to see if
  739.              the 4999,9999,9999/5000,0000,0000 boundary had been crossed. If
  740.              OF = 1 at the end, then the result was either too negative or too
  741.              positive.
  742.  
  743.  
  744.  
  745.              OVERFLOW
  746.  
  747.              How does the machine decide that overflow has occured? First,
  748.              what exactly is overflow and when is it possible for overflow to
  749.              occur?
  750.  
  751.              Overflow is when the result of a signed addition or subtraction
  752.              is either larger than the largest positive number or more
  753.              negative than the most negative number. In the case of the 4
  754.              digit machine, larger than +4999 or more negative than -5000.
  755.  
  756.              If one number is negative and the other is positive, it is not
  757.              possible for overflow to occur. Take +32 and -4791 as examples.
  758.              If we start with the positive number (+32) and add the negative
  759.              number (-4791), the result can't possibly be too positive.
  760.              Similarly, if we start with the negative number (-4791) and add
  761.              the positive number (+32), the result can't be too negative.
  762.              Therefore, the result can be neither too positive nor too
  763.              negative. Make sure you understand this before going on. 
  764.  
  765.              What if both are positive? Then overflow is possible. Here are
  766.              some examples:
  767.  
  768.  
  769.  
  770.  
  771.  
  772.              Chapter 0.1 - Numbers and Arithmetic                         xiii
  773.              ____________________________________
  774.  
  775.                  (+3500) + (+4500) = 8000 = -2000
  776.                  (+2872) + (+2872) = 5744 = -4256
  777.                  (+1799) + (+4157) = 5956 = -4044
  778.  
  779.              In each case, two positive numbers give a negative result. How
  780.              about two negative numbers?
  781.  
  782.                                 (7154) + (6000) = 3154 = +3154
  783.              (actual value)     -2946    -4000
  784.  
  785.                                 (5387) + (5826) = 1213 = +1213
  786.              (actual value)     -4613    -4174
  787.  
  788.                                 (8053) + (6191) = 4244 = +4244
  789.              (actual value)     -1947    -3809
  790.  
  791.              The numbers underneath are the negative numbers that the numbers
  792.              above them represent. In these cases, adding two negative numbers
  793.              gives a positive result.
  794.  
  795.              This is what the machine checks for. Before the addition, it
  796.              checks the signs of the numbers. If the signs are the same, then
  797.              the result must also be the same sign or overflow has
  798.              occurred.{10}  Thus + and + must have a + result; - and - must
  799.              have a - result. If not, OF (the overflow flag) is set (OF = 1).
  800.              Otherwise OF is cleared (OF = 0).
  801.  
  802.               
  803.              MULTIPLICATION
  804.  
  805.              Unsigned multiplication is easy. The machine simply multiplies
  806.              the two numbers. Since the result can be up to 8 digits (the
  807.              maximum result is 9999 X 9999 = 9998 0001) the machine uses two
  808.              registers to hold the result. We'll call them R1 and R2.
  809.  
  810.                  5436 X 174     R1   0094
  811.                                 R2   5864
  812.  
  813.                  2641 X 2003    R1   0528
  814.                                 R2   9923
  815.  
  816.              You need to know which register holds which half of the result,
  817.              but besides that, everything is straightforward. On this machine
  818.              R1 holds the left four digits and R2 holds the right four digits.
  819.  
  820.              Notice that our machine has changed the modular base from N to
  821.              N*N (from 1 0000 to 1 0000 0000). What this means is that two
  822.              things which are modularly equivalent under addition and
  823.              subtraction are not necessarily equivalent under multiplication
  824.              and division.  6281 and -3719 will not work the same.
  825.              ____________________
  826.  
  827.                 10. The machine checks something considerably more obscure
  828.              because it is easier to implement in semiconductor logic, but
  829.              what it is actually doing is checking to see if the two numbers
  830.              being added have the same sign. If they do, the result must be
  831.              the same sign or overflow has occurred.
  832.  
  833.  
  834.  
  835.  
  836.              The PC Assembler Tutor                                        xiv
  837.              ______________________
  838.  
  839.  
  840.              The machine can't do signed multiplication. What it actually does
  841.              is convert the numbers to positive numbers (if necessary),
  842.              perform unsigned multiplication, and then do sign adjustment of
  843.              the results (if necessary). It uses 2 registers for the result.
  844.  
  845.                            SIGNED MULTIPLICATION      REGS         RESULT
  846.  
  847.              (number)           (5372) X (3195)     R1   8521  =  -1478 6460
  848.              (actual value)     -4628  X +3195      R2   3540
  849.  
  850.              (number)           (9164) X (8746)     R1   0104  =   +104 8344
  851.              (actual value)      -836  X -1254      R2   8344
  852.  
  853.              (number)           (9927) X (0013)     R1   9999  =        -949
  854.              (actual value)      -73  X   +13       R2   9051
  855.  
  856.              Looking at the last example, if we performed unsigned
  857.              multiplication on those two numbers, we would have
  858.              9927 X 0013 = 0012 9051, a completely different answer from the
  859.              one we got. Therefore, whenever you do multiplication, you have
  860.              to tell the machine whether you want unsigned or signed
  861.              multiplication.
  862.  
  863.  
  864.              DIVISION
  865.  
  866.              Unsigned division is easy too. The machine divides one number by
  867.              the other, puts the quotient in one register and the remainder in
  868.              another. Once again, the only problem is remembering which
  869.              register has the quotient and which register has the remainder.
  870.              For us, the quotient is R1 and the remainder is R2.
  871.  
  872.                  6190 / 372          R1   0016           16  remainder 238
  873.                                      R2   0238
  874.  
  875.                  9845 / 11           R1   0895           895 remainder 0
  876.                                      R2   0000
  877.  
  878.              As with multiplication, signed division is handled by the machine
  879.              changing all numbers to positive numbers, performing unsigned
  880.              division, then putting back the appropriate signs.
  881.  
  882.  
  883.                          SIGNED DIVISION         REGS            RESULT
  884.  
  885.              (number)      (7192) / (9164)     R1   0003      +3  rem. -300
  886.              (actual value)-2808  /  -836      R2   9700
  887.  
  888.              (number)      (3753) / (9115)     R1   9996      -4  rem. +213
  889.              (actual value)+3753  /  -885      R2   0213
  890.  
  891.              Looking at the last example, 3753 / 9115, if that were unsigned
  892.              multiplication the answer would be 0 remainder 3753, a completely
  893.              different answer from the signed division. Every time you do a
  894.              division, you have to state whether you want unsigned or signed
  895.              division.
  896.  
  897.